﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace EfSandbox.Entities.Final.Common.Mappings {
    public class CountryMap : EntityTypeConfiguration<Country> {
        public CountryMap() {
            // Table
            ToTable("fin.com_countries");

            // Primary Key
            HasKey(c => c.CountryId);

            // Index
            Property(c => c.CountryCode)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_COM_CNT_CD", 1) { IsUnique = true }));

            Property(c => c.CountryShortCode)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_COM_CNT_CD", 2) { IsUnique = true }));

            // Attributes
            Property(c => c.CountryId)
                .HasColumnName("cnt_id");

            Property(c => c.CountryCode)
                .IsRequired()
                .IsUnicode(false)
                .HasMaxLength(25)
                .HasColumnName("cnt_cd");

            Property(c => c.CountryShortCode)
                .IsRequired()
                .IsUnicode(false)
                .HasMaxLength(25)
                .HasColumnName("cnt_short_cd");

            Property(c => c.CountryName)
                .IsRequired()
                .IsUnicode(true)
                .HasMaxLength(250)
                .HasColumnName("cnt_desc");

            Property(c => c.CountryNumericCode)
                .IsOptional()
                .HasColumnName("cnt_numeric_cd");

            Property(c => c.CurrencyCountryName)
                .IsOptional()
                .IsUnicode(true)
                .HasMaxLength(250)
                .HasColumnName("cnt_cur_desc");

            // Releationships
            // 0..1:n Currency
            Property(c => c.CurrencyId)
                .IsOptional()
                .HasColumnName("cnt_cur_id");
            HasOptional(c => c.Currency)
                .WithMany()
                .HasForeignKey(c => c.CurrencyId);
        }
    }
}
